WordPress How do I pass a variable from one add_action to another?

You shouldn’t be trying to pass the variable between actions. Variables do not persist across requests, and the first time you define it is when the checkout loaded, and the second time is after the checkout is submitted, which is a separate request entirely. To pass a value between requests you would need to store the value in the database, or in a cookie.

That’s the wrong approach, though. You don’t actually want the variable. What you want to know in each action is “is there a course in the cart?”. There’s no reason to only perform the check once and then try to pass the result around. Just perform the same check in both places:

/**
 * Check if the cart contains a product in the Courses category.
 */
function digitalessence_is_course_in_cart() {
    $course_in_cart = false;
    
    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
        if ( has_term( 'courses', 'product_cat', $cart_item['product_id'] ) ) {
            $course_in_cart = true;
            break;
        }
    }

    return $course_in_cart;
}

/**
 * Add a form field if the cart contains a product in the Courses category.
 */
function digitalessence_checkout_billing_form( $checkout ) {
    $course_in_cart = digitalessence_is_course_in_cart();

    if ( $course_in_cart ) {
        // Output form field.
    }
}
add_action( 'woocommerce_after_checkout_billing_form', 'digitalessence_checkout_billing_form' );

/**
 * Validate the form field added if the cart contains a product in the Courses category.
 */
function digitalessence_checkout_validation( $data, $errors ) {
    $course_in_cart = digitalessence_is_course_in_cart();

    if ( $course_in_cart ) {
        // Validate form field.
        // $errors->add( 'ebike', 'For our legs, we need to know if you are bringing an e-bike!' );
    }
}
add_action( 'woocommerce_after_checkout_validation', 'digitalessence_checkout_validation', 10, 2 );

Note that I’ve changed your last function to use the woocommerce_after_checkout_validation hook. If you want to validate a checkout field, this is the hook to use. If the validation fails add an error to the WP_Error object that’s passed as the second argument. My code includes an example commented out.