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)));
}