NEW YORK — Amazon is buying Whole Foods in a deal valued at about $13.7 billion, a stunning move into brick-and-mortar retail that sets the stage for more radical store experimentation and intensified competition with grocery rivals.

The deal unites the online juggernaut with the grocery store chain that fell behind as the organic and natural foods it helped popularize expanded to more locations and shoppers found “good enough” alternatives.

Whole Foods has long had a presence in the North Bay, starting with Marin County, where it has stores in Mill Valley and San Rafael. Years later the natural-foods goliath added a second Mill Valley store on East Blithedale Avenue, just off the Highway 101 exit, and a store in Novato, which struggled for years to find its market.

In Sonoma County in 2000, Whole Foods acquired for $25 million a three-store chain called Food for Thought, which had stores in Santa Rosa, Petaluma and Sebastopol. Food for Thought had been created by Ernie and Jim Shelton over the previous eight years, starting with a store in Sebastopol.

Whole Foods also has stores in Sonoma and Napa.

Amazon already offers grocery-delivery services in five markets, but the Whole Foods purchase would let it expand to many more. Amazon also offers grocery shipments elsewhere, but that’s tough with perishable foods.

The deal has the possibility to be “transformative,” Moody’s lead retail analyst Charlie O’Shea said in a note, “not just for food retail, but for retail in general.”

The “implications ripple far beyond the food segment, where dominant players like Walmart, Kroger, Costco, and Target now have to look over their shoulders at the Amazon train coming down the tracks,” O’Shea said.

The deal comes a month after Whole Foods announced a board shake-up and cost-cutting plan amid falling sales. The grocery store operator was also under pressure from activist investor Jana Partners. It also had been facing increased pressure from rivals, including European grocery chain Lidl, which is planning to enter the East Coast market, along with Aldi and Trader Joe’s.

Amazon, meanwhile, has been expanding its reach in goods, services, and entertainment. It’s also been testing automation technology at a Seattle convenience store that’s currently open only to Amazon employees. The store uses sensors to track items as shoppers put them into baskets or return them to the shelf. The shopper’s Amazon account gets automatically charged.

Amazon could cut costs if the technology gets good enough to deploy at Whole Foods locations. Whole Foods will keep operating stores under its name and John Mackey will stay as CEO, with headquarters in Austin, Texas.

Whole Foods, founded in 1978, has struggled to differentiate itself as competitors also now offer a plethora of fresh and organic foods. In addition to other natural and organic grocers, it has cited pressure from restaurant chains, meal-delivery companies and traditional supermarkets such as Kroger.

The deal is expected to close in the second half of 2017.

James Dunn of the North Bay Business Journal contributed to this report.

CORRECTION: Whole Foods Market has stores in Sonoma city and Fairfield, Conn. The original story omitted the former and put the latter in California.