Dallas Keuchel did what he usually does to the Yankees. The Astros’ lefty beat them, 3-2, Thursday night at the Stadium in a matchup of first-place teams that ended with the potential tying run being thrown out at the plate.

The Yankees, trailing 3-1 entering the bottom of the ninth, rallied as Aaron Hicks drew a one-out walk against closer Ken Giles. Pinch hitter Brett Gardner struck out looking, but Jacoby Ellsbury lined a single down the leftfield line to put runners on the corners.

Ellsbury stole second before Gary Sanchez hit a bullet to left. Hicks scored and Joe Espada waved in Ellsbury, who was thrown out on a bang-bang play by Jake Marisnick, with Brian McCann making the tag.

“You have to take that shot,” Girardi said of Espada’s call. “If there’s less than two outs, you don’t do it, but it’s the right call, and he made a perfect throw.”

McCann, whom the Yankees traded in the offseason, saw it the same way. “You have to send him there,” he said. “It was right on the money. I couldn’t ask for a better throw.”

Unlike past games against Keuchel, the Yankees (21-11) actually had their share of opportunities, mainly late. Still, the key hit eluded them as they went 0-for-5 with runners in scoring position against Keuchel over six innings, stranding six. Five came in the fifth and sixth. Overall, the Yankees went 1-for-9 with RISP and stranded nine.

advertisement | advertise on newsday

“He really knows how to pitch,” said Starlin Castro, who had a hit off Keuchel. “He commands all four of his pitches and hits the corners. He’s really good.”

Keuchel (6-0, 1.69) allowed one run (unearned), five hits and a walk and struck out nine. He came in with a 1.42 ERA in six starts against the Yankees, including a victory in the 2015 wild-card game, when he tossed six shutout innings.

“Maybe I just like the New York lights,” Keuchel said.

The AL West-leading Astros (24-11) got a two-run homer in the first by Carlos Correa. It came after Josh Reddick’s pop landed in short center for a double between Didi Gregorius, trying for an over-the-shoulder catch, and Ellsbury.

Girardi said it should have been caught by Ellsbury, who never called Gregorius off. “Looking back, I probably should have called it,” Ellsbury said. Said Gregorius: “I always go out until [an outfielder] calls me off. He didn’t call me off.”

Yankees videos

George Springer’s two-out single in the fifth made it 3-0 against Michael Pineda, who pitched reasonably well. Pineda (3-2, 3.27) allowed three runs, six hits and a walk in 6 2⁄3 innings. He struck out seven, giving him 50 strikeouts and six walks in 41 1⁄3 innings this season.

Keuchel allowed two hits in dominating the Yankees over the first four innings with only 42 pitches. An error opened the door in a 31-pitch fifth. The Yankees loaded the bases with one out, but got only one run. It came when Ellsbury drew a catcher’s interference, the 28th time he’s done so, one shy of Pete Rose’s record.

Matt Holliday singled to start the sixth and Castro singled off the leftfield wall to put runners at the corners, none out. But Keuchel struck out Aaron Judge, in a 2-for-14 slide, swinging at a 2-and-2 slider. Chase Headley went down swinging at a 2-and-2 changeup, and Gregorius struck out swinging at a 1-and-2 slider.

“We did a much better job, fifth and six, making him work,” Girardi said. “We just never real ly got the one big hit when we had a couple chances off him.”

With Roger Rubin