State to buy 20 commuter-rail locomotives from company it once spurned