Exporting mysql query result to .txt file
You need to embrace the filepath in quotes. This works for me (tested on MariaDB 10.4, but should work on older versions as well):

DELIMITER //
CREATE PROCEDURE test(filepath text, _query text)
BEGIN
  DECLARE stmt TEXT;
  SET stmt := CONCAT(_query, ' into OUTFILE \'' , filepath, '\''); 
  PREPARE s FROM stmt;
  EXECUTE s;
  DEALLOCATE PREPARE s;
END // 
DELIMITER ;


By the way, it's not good practice to use user-defined variables inside stored programs, like @sql in your procedure, because they retain their values between calls. (I think it will be OK in your procedure, though, unless you use @sql elsewhere.) DECLARE your variables instead.