For a freight train you absolutely couldn’t. Even high power lines aren’t powerful enough to power the electric motors on a locomotive through the standard way that things like light rails operate.
That locomotive example I used has a 4.5MW electric motor output. It would be next to impossible to get 4.5MW from the line to the motor using a third rail or something. The power draw would be too great for a freight train with say 6 locomotives. I live 50 feet from a rail line and 6 locomotives is about the average I see per train.
And just for scale, there are over 26,000 Class I locomotives like that in service. If each one ran for only 12 hours per day on average, that would eat up half a trillion kWh of power per year. That would be 12% of the total US electricity production per year, assuming no losses in transmission or efficiency.
It’ll become rarer, but will never go away. As long as diesel is being made, heavier duty trucks will have a diesel option.
Even if it becomes a solely biodiesel option. You’re just not going to beat the efficiency, energy density, and quick refueling of an internal combustion engine until you can have a battery 1/15 the mass for the same energy, and can charge the thing in under half an hour, and doesn’t cost more than the vehicle itself when it’s time to replace it.
ICE engines, and diesels in particular can run for millions of miles. The record for a semi mileage is just over 3 million miles on the engine. You’re not going to find a battery pack that can go anywhere remotely close to that long. Especially in a heavy use vehicle like a truck that will be constantly going through charge cycles.
Just looking at the Tesla semi, the 500 mile range battery is 900kWh. A 100kWh model S battery costs $15,000 to replace, with $13,500 of that being the battery itself. Scaled up, the semi battery would be in the $90-100k range to replace.
The average semi runs around 100,000 miles per year. If you can get 1,000 full charge cycles out of the battery, you’d be replacing one every 5 years to the tune of nearly $100k each time. Not to mention replacing the electric motors themselves at several grand pop, and those don’t tend to last as long as the battery. Especially in a truck hauling 82,000lbs.