Sonny Gray of the Yankees is removed from a game...

Sonny Gray of the Yankees is removed from a game against the Rays at Yankee Stadium on Sept. 28, 2017. Credit: Jim McIsaac

Sometimes you’re Sonny, and sometimes you’re Gray.

Things had been going so well for the Yankees lately that it could be all too easy to forget that they, too, could be susceptible to games like Thursday night’s. When a dark cloud casts a pallor on the happy, sun-kissed days that have dominated this warm autumn at the Stadium.

You can hit four home runs, but your pitching can still fall apart, which led to a seven-run fifth inning in a 9-6 loss to the Rays. Sonny Gray can struggle, the bullpen can do the same, and the team that looked unbeatable Wednesday can look pedestrian the next game.

Although the Red Sox lost, 12-2, to the Astros, Boston’s magic number to clinch the AL East dwindled to one with three to play.

“It’s frustrating because we had a chance to pick up a game,” Joe Girardi said. “It just wasn’t meant to be tonight. [Gray] did struggle with his command at times tonight . . . We just couldn’t seem to get the third out in that fifth inning.”

The Yankees saw a three-run lead evaporate behind yet another uneven home performance by Gray (10-12, 3.55). He pitched 4 2⁄3 innings, allowing six runs, six hits, five walks, a wild pitch and two home runs. Gray has allowed 15 earned runs in his last three home games, with six homers. Working without Chad Green and Dellin Betances, both of whom pitched Wednesday, the shorthanded bullpen faltered behind Gray, allowing two more runs in the seven-run fifth and another in the sixth.

Things disintegrated at a breakneck pace in that big inning. Holding on to a 4-1 lead, Gray was undone by his wildness, along with Gary Sanchez’s inability to rein him in. With runners at the corners and one out, Gray sailed an off-speed pitch by Sanchez to score Mallex Smith, and Sanchez’s wild throw back home allowed Corey Dickerson to reach third.

Up to that point, Gray spent the game getting hit hard, but to little adverse effect. No longer. Sanchez’s MLB-worst 16th passed ball let Dickerson score before Lucas Duda walked. Then came a two-run, go-ahead homer by Wilson Ramos, on a hanging curve. Gray allowed one more single before getting pulled for Jonathan Holder, who didn’t fare much better.

Holder gave up three more runs, one charged to Gray, as the Rays exploded for the 8-4 lead. In the sixth, Chasen Shreve let up a solo home run, an absolute blast to the second deck in left by pinch hitter Trevor Plouffe — to make it 9-4.

“I didn’t throw enough strikes,” Gray said. “It all seemed to catch up with me in that fifth. I was kind of all over the place all night, so I made it really hard on Gary from the first inning.

“It’s over. The quicker I can put this behind me, the quicker I can move on.”

Before that, it was business as usual for the Yankees — pummeling teams with their big, loud bats. Dickerson homered on Gray’s fourth pitch of the night, but the deficit lasted about as long as it takes an Aaron Judge projectile to exit the park. Brett Gardner and Judge hit back-to-back homers in the first, the first time the Yankees did that to lead off a game since April 16, 2012 (Derek Jeter, Curtis Granderson).

Judge’s 51st homer extended his rookie record. Greg Bird hit a solo home run in the fourth, his second in two days. Aaron Hicks added a solo shot in the ninth, his second in two games.

Warren returns. Girardi said the Yankees would activate reliever Adam Warren (back) from the disabled list Friday. “I think it’s important to get him in a game or two to see where he’s at” before deciding how to use him in the playoffs, Girardi said.

More Yankees headlines

Newsday LogoSUBSCRIBEUnlimited Digital AccessOnly 25¢for 5 months
ACT NOWSALE ENDS SOON | CANCEL ANYTIME