The free UK DEM data from OS is at too low a resolution to give anything near accurate results, you might get away with it in the flat lands of the Cambridgeshire Fens (where I live now) but in Yorkshire (where I lived for many years) a large proportion of the land is relatively steep hills and valleys which throws the low res DEM data right off. Also, because of the nature of the terrain, much of the railway is in tunnels, cuttings or on embankments.
I speak from practical experience because, not that long ago, I tried to define accurate levels for the line between Skipton and Leeds (which still exists so that helps). Starting with a DEM using the OS data and comparing it with spot heights deduced from OS paper mapping and also cross checking Google and any other sources I could find including gradient diagrams I soon discovered that even just doing this short stretch of line with any degree of accuracy would be a mammoth task. Doing the whole of Yorkshire (especially as it was at the peak of development) would be near impossible.
More accurate DEM data is available but, at least for me, the cost was prohibitive (£ several hundreds) and Transdem (then) was not able to handle the data - although it may now.
The only other dead accurate source of data, for existing lines only, that I can think of would be from the track measurement vehicles, but no doubt that data will be regarded by Network Rail as a state secret.
Chris