AstraZeneca has agreed to buy U.S. Alexion Pharmaceuticals for $39 billion, betting on rare diseases and immunology as the British drug company emerges as one of the leading developers of a COVID-19 vaccine.