Eli Lilly & Co. agreed to buy AtaiBeckley Inc. for as much as $3.8 billion, underscoring growing interest from large drugmakers in the once-fringe area of psychedelic medicine.