Iberdrola SA, Spain’s largest electricity company, agreed to buy UIL Holdings Corp. in a deal valued at about $3 billion in cash and shares to create a U.S. utility with about 3.1 million customers.