Trains are an experimental feature that was added in 24.0 for private lobbies and single player.

Factories spawn trains and railroads automatically when two or more buildings have been placed within a reasonable distance on land.
For each building the train visits, you get gold, 10,000 for owned/unallied or 35,000 if allied.
Trains are spawned according to a probabilistic mechanism that is influenced by the number of available stations in the connected cluster. The probability to spawn a train increases with the number of eligible stations within the cluster according to this function:

trainSpawnRate(numberOfStations: number): number {
return Math.min(1400, Math.round(20 * Math.pow(numberOfStations, 0.5)));
}
Since Update 35.0, a player’s first few factories spawn trains faster: the same small-economy boost as trade ships (1.5× at one factory level, fading out by 6). The v35 spawn rate is max(1, floor((factories + 10) × 15 / (saturation × boost))), where the saturation curve damps spawning past about 560 trains in the world. A station built on a stretch of track a train was already on now pays gold correctly.