I'm so glad that you mentioned railroads because there is a great book, Railroads and Regulation by Gabriel Kolko about the capitalist anti-competiton regulation of the railroad industry that caused their concentration. If you wanna read about it, an essay about it is called "Big Business and the Rise of American Statism" inside of Markets Not Capitalism (freely available online). I'll summarize what it says tho:
Every time big railroad magnates tried to form a cartel to fix prices, a smaller competitor would lower rates and steal all the customers; freight rates went wayy down in this time period. The big railroad owners (like JP Morgan's clients) lobbied for the ICC not to regulate them, but to regulate their competitors. They wanted the government to make price-cutting illegal (calling it rebates or discrimination).
Regarding sanitary packages, the essay _also addresses this_: the big Chicago meatpackers supported regulations because the compliance costs were so high they drove small local butchers and slaughterhouses out of business. The "sanitary" laws were a weapon to kill local competition, not a way to keep food safe
Before even railroads, the original manufactory boom that is commonly associated with the industrial revolution is the exact kind of case the other poster is alluding to when thinking of large economies of scale. I think Kolko is great to demonstrate that economies of scale aren't infinite, and in the absense of government intervention, market forces actively conspire to ensure firms shrink to their optimal size. But that also proves the flip side: firms will grow to their optimal size too. There is no economic reason to be suspicious of large firm sizes, and the political reasons inevitably hinge on some sort of mistaken assumption of the economics of the matter; anti-trust regulations advocacy is a perfect example of this.
But they did have the unintended consequence of keeping food safe.
I would argue that consolidation happened because rail is a capital-intensive natural monopoly. It's easy to dominate a region just by buying up lines. Smaller lines couldn't steal customers from big players because no other lines were physically able to offer the same service. Trucking is another story. There, the highways are natural monopolies, and trucks are an over-the-top service.