The banking industry is heading into a difficult environment of slimmer margins and a slower economy and that's why lenders are teaming up, analysts said.

On Thursday, BB&T announced it will buy SunTrust Banks for $28.24 billion in an all-stock deal, or a 7 percent premium of $62.85 per share. The merged company will be the sixth-largest bank in the U.S., bringing its assets to around $442 billion. The deal is also the biggest for the industry since the financial crisis.

"BBT/STI deal an incremental positive for both companies, and is what the sector needs given tougher growth outlook over the near to intermediate term," Baird analyst David George wrote in a note. "We believe the industry needs more low premium deals in order to reduce the overcapacity in the sector in a shareholder friendly way. This deal accomplishes that."

The banking sector is facing a slew of challenges, most notably a crunch in their margins evident in the flattening of the so-called yield curve. The spread between the 10-year and 2-year Treasury yields is around 16.8 basis points. A flat yield curve can signal a drop in profitability for the industry. A bank makes money on the difference between what it charges for long-term loans and the rate it gives out on deposits.

Bank shares fell sharply last year as investors anticipated this difficult environment The SPDR S&P Bank exchange-traded fund (KBE) dropped 21.1 percent in 2018. The KRE, which tracks only regional banks, fell 20 percent last year. SunTrust and BB&T shares pulled back 21.9 percent and 12.9 percent last year, respectively.

"With our view that banks are nearing peak returns, this is one of the few remaining bull arguments for the group," an analyst at Citi said in a note.

Another reason banks are joining forces is the digitization of banking, according to RBC Capital Markets analyst Gerard Cassidy.

In an interview Thursday on CNBC's "Squawk Box," Cassidy said, "The technology spending is one of the reasons why I think you're going to see these mergers continue on a go-forward basis."

Despite the tough outlook for the industry, analysts believe this merger wave could drive a comeback in the shares this year. Banking mergers totaled just $19.7 billion last year, the lowest in more than two decades, according to Refinitiv.

"Our bank theme is that 'Goliath is Winning' – today's MOE (merger of equals) seems driven by the theme that scale matters more than ever before," wrote Mike Mayo, banking analyst at Wells Fargo Securities. "We are buyers of STI and BBT."