An echo line in a transition_post_status action leads to “cannot modify header information – headers already sent by”

The error message you are seeing is normal for that operation. By the time the transition_post_status hook fires, the HTTP page headers is already sent. By simply echoing an error message inside a hook like transition_post_status which was never intended to echo any output to the browser, as you did, you are actually trying to insert your error message into this page headers, causing the error message to show up.

transition_post_status should be used to fire a hooked function on success or to performs a specific task, and not to echo output to the browser. The following is untested, but you can try something like this

add_action( 'transition_post_status', function ( $new_status, $old_status, $post ) {
    if( $new_status === 'publish' ) {
        add_action( 'admin_head', function () {
            echo 'To test if we get this message without headers already sent error message';
        }, PHP_INT_MAX );
    }, 10, 3 );