To summarize and expound upon what has been said in earlier answers:
It has to do with race, ethnicity and music history. Rock is a derivative form of music mostly created by white people in England and the USA from the late 1950s forward. The clearly-acknowledged source material for white rock musicians in the 1950s and 1960s was the music of black Americans from previous decades: blues, rhythm & blues, gospel and rural and urban folk music of black people, from the late 1800s up through the beginning of rock and roll itself in the 1950s and 1960s.
Early rock songs borrowed themes from African-American music from earlier decades, and in many cases early rock songs were simply cover versions of existing black music in more of a rhythm and blues genre. One of these prominent themes was that of trains.
Besides this, however, references to trains are found in other kinds of music of white people in the USA and England going back to, well, the invention of trains in the early 1800s. There are numerous references to trains in old-time Appalacian music and in bluegrass and country music throughout this long time-period.
So no, the use of trains and travel by train as a theme is not particular to rock music at all. It is found in all the earlier kinds of music that rock and roll music paid tribute to.
The clearest example I can think of is that of the song "Train Kept A-Rollin'", released in 1951 by the black musician Tiny Bradshaw, in the jump-blues style.
This record was a minor hit, but it was only known among black Americans, because the record of that song was only marketed and sold in black communities and in stores that only sold records to black customers. It was only broadcast on radio stations that catered exclusively to black listeners. This was the reality of the segregationist society in the USA in the early 1950s.
However, in 1956, a group of white musicians named Johnny Burnette and the Rock and Roll Trio recorded the song in New York and the record was marketed to white record stores and white radio stations, becoming a much larger hit. Johnny Burnette and the Rock and Roll Trio are considered to be among the very first rock bands. Then, in 1965, the British white rock band The Yardbirds made a hugely successful cover-version of this song. Led Zeppelin performed this song in concert. In 1976, the American white rock band Aerosmith made another cover-version of this song, and this version became a standard among rock bands.
So with this example you can see how the theme and the music progressed from a black source that pre-dates rock and roll and carries through to the classic rock era.