Understanding concepts iro drawing a rail line starting and ending at two given stations

I am very new to overpass.

It seems to me that a line is a set of ways all with the same ref tag value (they are also all railway tag - rail). For example:

[out:json][timeout:300];(
way[“ref”=“431000”];
);
out geom;

a) I am struggling to understand why some ways making up this line have just one or two nodes to define them and some have a huge number of nodes.
b) It seems to me that all of these nodes are tagless nodes, with just coordinates, which leaves me at a loss to know how a rail line’s ways’ nodes can somehow be associated with stations as would be necessary to selectively draw a line from one station to another. The ways’ nodes and the station nodes seem quite separate things.

My thinking had been to identify a station node and from that somehow identify the set of ways that make up the line this station is on.

However, taking the set of ways in the example above, if I quite separately identify a station I know to be on this real world line and its node id that id is not amongst any of the nodes included in the ways that make up that line, only the tagless nodes.

I realise I have more understanding to gain but until I can get past any misconceptions / misunderstandings at this point then I am a bit blocked.

My thinking had been to identify a station node and from that somehow identify the set of ways that make up the line this station is on.

yes, for station nodes you would have to either check memberships in route relations or search for nearby rails. Sometimes there are station polygons. There are also railway=stop nodes on the rails (sometimes). In case you aren’t aware, taginfo is very useful to explore how frequently tags are used, and conveniently links to the definitions in the wiki.
E.g. railway nodes: https://taginfo.openstreetmap.org/keys/railway?filter=nodes#values

Thank you so much @dieterdreist - I need to think this through further (very new) but I think this is very helpful.

Just before you posted, having thought a little more, I was about to say I would search for nearest (I presume there is an operator for this) ways to starting station and ending station then pick the ways that had a common ref and search for those ways in a bounded square formed from parts of both stations coordinate parts bottom most and left most then top most and right most values. A quick and dirty proof of concept seemed to suggest this may work.

However this does not feel very elegant and may suffer from performance issues and multiple way sets in some cases (though that may be valid).

I am interested in your suggestion to check memberships in route relations - can you possibly elaborate or point me to some more reading on this? I have started by looked at nodes and ways but am still a bit confused about relations.

Clearly the queries can be very powerful, but I think I am going to need a lot more than my first day of looking to get a fuller understanding.