WARNING: The following contains spoilers for Demon Slayer: Kimetsu no Yaiba the Movie: Mugen Train.
Death is inescapable in the world of Demon Slayer as the titular slayers continue their fight against the flesh-eating demons created by Muzan, the original Demon. But the death of Kyojuro Rengoku in Demon Slayer: Mugen Train is particularly poignant as the first on-screen death of a Hashira in the series. The Flame-Breathing Hashira was the least eccentric of his comrades, with a more earnest and noble personality, making his gruesome demise that much more tragic. But it was also necessary for the series' overall narrative, of which Mugen Train is canonically part of.
Sometimes the story demands that a character meet their end, regardless of how well-received they have been by the audience. Rengoku's relationship with the protagonist, Tanjiro, ultimately doomed the Hashira. After all, Demon Slayer is Tanjiro's story and Rengoku was a bit too convenient to keep around as his mentor. As a master of Flame-Breathing, Rengoku may have been able to help Tanjiro master the Hinokami Kagura, an incredibly powerful technique that appears to be related to Flame-Breathing.
Unfortunately, the events of Mugen Train happen too early in the series' timeline for Tanjiro to master such a powerful technique. In avoiding the common anime trope of an overpowered protagonist, Rengoku, sadly, had to be taken off the board. Though he emphatically declares his willingness to train the young Demon Slayer, this would have made Tanjiro's journey just a tad too easy, which ultimately would have hurt the series more than Rengoku's passing ever could.
If Enmu had been the final villain of Mugen Train, the series perhaps could have found a compelling way to keep Rengoku around without his mentorship becoming too convenient for Tanjiro. It has been established that Rengoku has a younger brother whom he cares for deeply, and the Hashira are constantly being given assignments from Kagaya Ubuyashiki, leader of the Demon Slayer Corps. So, there were certainly ways Rengoku could have been written out of Tanjiro's journey without being killed off.
However, the arrival of Akaza at the end of Mugen Train necessitated the Flame-Breathing Hashira's death. Akaza, Upper-Rank Three of the 12 Kizuki, is the first of Muzan's most elite group of Demons to appear in the series. As such, his entrance needed to be a definitive demonstration of the Demons' obscene power and the threat that the Upper Ranks pose. It would have hurt the credibility of the remaining Upper Ranks, and even Muzan, if Rengoku had triumphed or even survived this encounter.
The Hashira of the Demon Slayer Corps are meant to be the best of the best, as they have been seen effortlessly defeating Demons that Tanjiro, Inosuke and Zenitsu had nearly died battling. While they were lucky to have had a Hashira with them when Akaza appeared, it was important to establish that the Hashira are not invincible. Tanjiro and the others learned that they can't rely on the Hashira to save them each time they find themselves outmatched by a Demon.
Things had been sailing just a little too smoothly for Tanjiro and company as of late. Once he, Inosuke, and Zenitsu had truly become lifelong friends, and gotten significantly stronger, it was time for a reality check. Rengoku's death provided the trios with new motivation to not only get stronger but to continue to grow as individuals, now that they carry Rengoku's legacy with them.