Is this a correct usage of ob_start() in my WordPress project?

My question is, should I be doing it this way?

Yes, but only if the other possible/better fixes do not work for you.

You can also read this article about troubleshooting common WordPress errors such as the “headers already sent” error.

And when developing a plugin/theme, I suggest you to turn on WordPress debugging because normally, you would be able to identify the source of an error by (simply) checking the error_log or wp-content/debug.log file for entries/lines relevant to the error. 🙂

And here’s a working example as a reference when adding custom WooCommerce endpoint:

Here’s the code which adds a /company-profile endpoint to the WooCommerce “My Account” endpoint; so you’d have example.com/my-account/company-profile, which simply displays a form with a field to set/update the user’s company ID (registration number..), and upon successful update, the user is sent back to the “My Account” page.

And if you look at that code and try it with a default WordPress installation — with just WooCommerce as the only active plugin, you should see that there’s no output being generated prior to performing the redirection.