You can further optimize the setup by not installing engines/motors in all of them. So maybe you have one car providing locomotion, with the rest following behind and designed for carrying.
That’s actually getting less common; pretty much all rapid transport and commuter trains are multiple units these days, as are an increasing number of intercity trains.
In Ireland, there are precisely two passenger routes still operated with locomotives, and there’s a tender offer out to replace one of them with a (really wacky; diesel, battery, _and_ overhead lines in two voltages!) multiple unit.
And all the power could just come from a few large centralized facilities that are super efficient. We could just use thin strands of metal to get it to the vehicles over head…
The economics work out where they’re pretty low-frequency (I think less than two an hour per direction is the usual figure).
They’re also useful as a transition technology. The DART+ project in Ireland will use them for one line which will have the frequency for electrification (8 trains per direction per hour) and is already partially electrified, but is going to take a while to fully electrify (due to low bridges etc); once it’s electrified they’ll then likely be used in low-frequency regional routes.
(The realised project will use 750 uniform cars, about 200 of which will have batteries.)
They are good for infrequently used track and places where overhead wires would be in the way, like that very Tesla employee shuttle on it's own track and container ports.
It's not the best way to go for mainline track and not suitable for long distance high speed trains.
Ireland is going to use a particularly unusual one for the Dublin-Belfast intercity route. It will have batteries, _and_ diesel generators, _and_ will run off overhead lines, in two voltages. The context is that parts of the line will take a while to electrify; it will initially run on overhead, battery, and diesel, then just overhead and battery as the lines are built out, and then hopefully finally just overhead.
Expense is correlative to scale, likely it's cheaper to deploy pantographs than battery factories.
Why did India build a high speed freight corridor with overhead power when they could have used batteries instead? Because the quantity of battery to power the trains doesn't exist, and overhead wires do.