Set a cookie inside the 404.php theme page

I think the template files load too late, try setting it in the functions.php file.

add_action( 'init', 'wpse325439_init' );
function wpse325439_init() {
    if ( !headers_sent() && is_404() ) {
        setcookie( 'name', 'carl', 0, '/names/', 'my.webdomain.com', isset($_SERVER["HTTPS"]), true);
    }
}